package cn.kgc.crowdfundingadminspringboot2.controller;

import cn.kgc.crowdfundingadminspringboot2.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadminspringboot2.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot2.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot2.service.AdminService;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.runtime.RewriteException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class AdminController {
    @Resource
    AdminService adminService;


    @RequestMapping("/admin/do/login")
    public String doLogin(String loginAcct,
                          String loginPwd,
                          HttpSession session,
                          Model model) {
        Admin admin = adminService.login(loginAcct, loginPwd);
        if (admin == null) {
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "index-page";
        }
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        return "redirect:/admin/to/main/page.html";
    }

    //跳转登录成功页面
    @RequestMapping("/admin/to/main/page.html")
    public String toLogin() {
        return "admin-main";
    }

    //退出登录
    @RequestMapping("/admin/logout.html")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }

    //分页
    @RequestMapping("/admin/query/for/search")
    public String queryForSearch(

            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value = "pageNum", defaultValue = "1",required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5",required = false) Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "",required = false) String keyword,
            Model model) {

        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        System.out.println(pageInfo.getPageNum());
        System.out.println(pageInfo.getPageSize());
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
      
        return "admin-page";
    }
    /**
     * 删除
     * @param adminIdList
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        try {
            adminService.batchRemove(adminIdList);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {

            return ResultEntity.failed(null, e.getMessage());
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/admin/to/add/page.html")
    public String  ADD(){
        return "admin-add";
    }
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/query/for/search";
    }
    /**
     * 修改
     *
     */
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Admin admin = adminService.selectById(adminId);

        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ATTR_ADMIN,admin);
        return "admin-edit";
    }
    @RequestMapping("/admin/update")
    public String   updateAdmin(Admin admin,Integer pageNum){
        adminService.updateAdmin(admin);

        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

}


